<!-- 用户展示列表 -->
<template>
  <div class="user">
    <div class="operation-area">
      <el-button type="primary" icon="el-icon-plus" @click="save">新增用户</el-button>
    </div>
    <!-- 搜索区域 -->
    <el-form :inline="true" :model="params" class="search-form">
      <el-form-item>
        <el-input v-model="params.empName" placeholder="员工姓名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="params.phoneNumber" placeholder="手机号码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="dataUserList">查询</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格区域 -->
    <el-table :data="employeesList" border style="width: 100%">
      <el-table-column prop="employeeId" label="ID" width="80"></el-table-column>
      <el-table-column prop="loginName" label="登录名"></el-table-column>
      <el-table-column prop="empName" label="员工姓名"></el-table-column>
      <el-table-column prop="position" label="职位"></el-table-column>
      <el-table-column prop="phoneNumber" label="手机号码"></el-table-column>
      <el-table-column prop="email" label="邮箱"></el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button 
            size="mini" 
            type="primary"
            @click="handleEdit(scope.row)"
          >编辑</el-button>
          <el-button 
            size="mini" 
            type="danger" 
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="params.pageNum"
      :page-size="5"
      layout="total, prev, pager, next"
      :total="total">
    </el-pagination>

    <!-- 编辑对话框 -->
    <el-dialog title="编辑用户" :visible.sync="dialogVisible" width="50%">
      <el-form :model="editForm" :rules="rules" ref="editForm" label-width="100px">
        <el-form-item label="登录名" prop="loginName">
          <el-input v-model="editForm.loginName"></el-input>
        </el-form-item>
        <el-form-item label="员工姓名" prop="empName">
          <el-input v-model="editForm.empName"></el-input>
        </el-form-item>
        <el-form-item label="职位" prop="position">
          <el-input v-model="editForm.position"></el-input>
        </el-form-item>
        <el-form-item label="手机号码" prop="phoneNumber">
          <el-input v-model="editForm.phoneNumber"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editForm.email"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getUserList, deleteUser, updateUser } from "@/api/user.js";

export default {
  name: "UserIndex",
  //import引入的组件需要注入到对象中才能使用

  components: {},
  data() {
    //这里存放数据
    return {
      params:{
        empName: '',
        phoneNumber: '',
        pageNum: 1
      },
      // 员工列表数据
      employeesList: [],
      // 总条数
      total: 0,
      // 添加新的数据项
      dialogVisible: false,
      editForm: {
        employeeId: '',
        loginName: '',
        empName: '',
        position: '',
        phoneNumber: '',
        email: ''
      },
      rules: {
        loginName: [
          { required: true, message: '请输入登录名', trigger: 'blur' }
        ],
        empName: [
          { required: true, message: '请输入员工姓名', trigger: 'blur' }
        ],
        position: [
          { required: true, message: '请输入职位', trigger: 'blur' }
        ],
        phoneNumber: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    //跳转到新增组件
    save() {
      this.$router.replace({
        path: "/user/addUser"
      });
    },
    //获取全部用户信息
    dataUserList() {
      getUserList(this.params).then((res) => {
        if (res.code === 200) {
          this.employeesList = res.data.list;
          this.total = res.data.total;
        } else {
          this.$message.error(res.message);
        }
      });
    },
    
    // 分页处理
    handleCurrentChange(val) {
      this.params.pageNum = val;
      this.dataUserList();
    },

    // 处理编辑
    handleEdit(row) {
      this.editForm = { ...row };
      this.dialogVisible = true;
    },

    // 提交编辑
    submitEdit() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          updateUser(this.editForm).then(res => {
            if (res.code === 200) {
              this.$message.success('更新成功');
              this.dialogVisible = false;
              this.dataUserList(); // 刷新列表
            } else {
              this.$message.error(res.message || '更新失败');
            }
          }).catch(err => {
            this.$message.error('更新失败：' + err.message);
          });
        }
      });
    },

    // 处理删除
    handleDelete(row) {
      this.$confirm('确认删除该用户吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteUser(row.employeeId).then(res => {
          if (res.code === 200) {
            this.$message.success('删除成功');
            this.dataUserList(); // 刷新列表
          } else {
            this.$message.error(res.message || '删除失败');
          }
        }).catch(err => {
          this.$message.error('删除失败：' + err.message);
        });
      }).catch(() => {
        this.$message.info('已取消删除');
      });
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.dataUserList();
  },
  
};
</script>
<style lang='less' scoped>
.user {
  padding: 20px;

  .operation-area {
    margin-bottom: 20px;
  }

  .search-form {
    margin-bottom: 20px;
  }

  .el-pagination {
    margin-top: 20px;
    text-align: right;
  }
}
</style>